//this file is part of ePopup
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#pragma once
#ifndef __AFXWIN_H__
	#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h"

#include <map>
#include <vector>
#include <sstream>
#include <Options.h>
#include <Logger.h>
#include "PopupClientInfo.h"

//===============================================================
// Options defines
//===============================================================

// Sameple of popup.ini
/*
###################################
# Mandatory options:
###################################

Resource path=C:\Documents and Settings\Guigui\Bureau\Popup\res
Server hostname=pcguigui
Server port=2000
User name=Svr

###################################
# Auto mode could be any mode:
#-----------------------------
# - normal   : display messages as they come
# - prompt   : prompts before displaying message
# - mute     : switch off Popup. Messages are queued.
# - textonly : displays only the text part of the message.
#
# Setting delay to 0 seconds means that auto mode
# is turned off
####################################

Auto mode=textonly
Auto mode delay=10
*/

// End of sample

#define POPUP_RES_PATH        "Resource path"
#define SERVER_HOSTNAME       "Server hostname"
#define SERVER_PORT           "Server port"
#define CLIENT_USER_NAME      "User name"
#define AUTO_MODE             "Auto mode"
#define AUTO_MODE_DELAY       "Auto mode delay"
#define WEB_BROWSER_PATH      "Web browser"
#define APP_ARGS_SUFFIX       "_args"
#define APP_ASSOCIATIONS_KEY  "extensions"
#define APP_DEFAULT_EXTENSION "default"

#define USER_NAME_MAX_LENGTH   64
#define HOSTNAME_MAX_LENGTH    64
#define APP_PATH_MAX_LENGTH   512

//===============================================================
// Forward declarations
//===============================================================

class  CpopupDlg;
struct Options;
struct ResourceEntry;
struct Logger;
class  PopupClient;
class  PopupServer;

//===============================================================
// Popup app class definition
//===============================================================

class CpopupApp : public CWinApp
{
public:
	CpopupApp(LPCTSTR lpszAppName = NULL);

	typedef std::map<CString, ResourceEntry*> PopupResources;

	// Main dialog
	CpopupDlg*      popupdlg;
	// Options
	Options         m_options;
	// Resources vector
	PopupResources  m_resources;
	// Logging stream
	Logger          m_logger;
	// Reference to popup client object
	PopupClient*    m_popupclient;
	// Reference to popup server object
	PopupServer*    m_popupserver;
	// Mutex to protect access to clients list
	static CMutex   s_clientmutex;

	// Paths to different folders
	CString         m_basedir;
	CString         m_userdir;
	CString         m_tempdir;
	CString         m_inifile;
	CString         m_hostname;
	CString         m_username;
	std::string		m_servername;
	int 			m_serverport;
	PopupClientMode m_automode;
	int             m_automodeDelay;
	bool            m_resetTimer;

	// Methods
	virtual BOOL InitInstance();
	void exitApp();
	
	// Assess "connected" users list
	bool getUsers(std::vector<PopupClientInfo> &users);
	ResourceEntry *getRandomResource();
	bool getApplicationByExtension(const std::string & p_extension, 
								   std::string & p_app, std::string & p_args, 
								   bool _setDefault = true);
	bool openFile(const std::string & p_file);
	bool openURL(const std::string & p_file);

protected:

	// Methods
	bool loadOptions();
	bool initUserDirectory();
	bool loadEnvironment();
	bool saveOptions();
	bool loadResources();
	bool restartNetwork();
	bool loadResourceDir(const CString & p_dir);
	bool dumpResources();

	friend class PopupPropertiesDlg;
};

// Main application instance
extern CpopupApp theApp;
